<?php

class docupload extends Controller{

	function index(){
		if(!$this->user->isLogged()){
			$this->response->redirect(MAIN_PAGE);
		}
		$this->load->model("dept");
		$this->data["dept_list"]=$this->model_dept->loaddepartment()->rows;
		$this->load->model("doctype");
		$this->data["doctype_list"]=$this->model_doctype->loaddoctype()->rows;
		$datetime=explode(" ",date("Y-m-d H:i:s"));
		$this->data["stime"]=$datetime[1];
		$this->document->setTitle("Document Management Solutions");
		$this->document->setSelectedMenu("menu_new");
		$this->document->addLink("jqueryui/jquery.ui.all.css");
		$this->document->addLink("jquery.timepicker.css");
		$this->document->scripts=array("jquery-ui/jquery-ui-1.8.16.custom.min.js","jquery.timepicker.min.js","date.format.js");
		$this->children=array("header","footer");
		$this->template="docupload.html";
		$this->response->setOutput($this->render(TRUE),0);
	}

	function docuploadpost(){
		$jsonarr=array();
		if($this->request->is_ajax_request() && $this->user->isLogged()){
			$sql="select * from document where document_no='{$this->db->escape($_POST["docno"])}'";
			$res=$this->db->query($sql);
			if($res->num_rows==0){
				$sql="insert into document(document_no,documenttype_id,docdate,doctime,fromaddress,referenceno,".
		     "issuedate,addressedto,receiptdate,receipttime,dateenteredby,departmentid,remark,".
		     "created_date,active_yn,user_id) values('{$this->db->escape($_POST["docno"])}','{$this->db->escape($_POST["doctypeid"])}".
		     "',{$this->convertdate($_POST["docdate"])},'{$this->db->escape($_POST["doctime"])}','{$this->db->escape($_POST["from"])}',".
		     "'{$this->db->escape($_POST["refno"])}',{$this->convertdate($_POST["issuedate"])},'{$this->db->escape($_POST["addressedto"])}',".
		     "{$this->convertdate($_POST["receiptdate"])},'{$this->db->escape($_POST["receipttime"])}',".
		     "'{$this->db->escape($_POST["dataenteredby"])}','{$this->db->escape($_POST["deptid"])}','{$this->db->escape($_POST["remark"])}',".
		     "NOW(),'Y','{$this->user->getId()}')";
				$this->db->query($sql);
				$jsonarr["documentid"]=$this->db->getLastId();
				$jsonarr["status"]=true;
				$jsonarr["msg"]="Document Added Sucessfully";
			}else{
			    $jsonarr["status"]=false;
			    $jsonarr["msg"]="Document No already available";
			}
		}
		else{
			$jsonarr["status"]=false;
			$jsonarr["redirect"]=true;
		}
		echo json_encode($jsonarr);
	}

	private function convertdate($d){
		return "STR_TO_DATE('{$d}','%m/%d/%Y')";
	}



}

?>